use std::io::stdin;

fn main() {
    loop {
        println!("您想知道第几项？");
        let mut n = String::new();
        stdin().read_line(&mut n).expect("无法读取输入");
        let n: u32 = n.trim().parse().expect("这不是一个有效的数字");
        println!("第{}项的值为{}", n, fibonacci_number(n));
        // println!("第{}项的值为{}",n ,fibonacci_number2(n) );
        println!("");
    }
}

fn fibonacci_number(n: u32) -> u128 {
    if n == 1 {
        0
    } else if n == 2 {
        1
    } else {
        let mut x = 3;
        let mut value_sub_2 = 0;
        let mut value_sub_1 = 1;
        let mut value = 0;
        while x <= n {
            value = value_sub_1 + value_sub_2;
            value_sub_2 = value_sub_1;
            value_sub_1 = value;
            x += 1;
        }
        value
    }
}

fn fibonacci_number2(n: u32) -> u32 {
    if n == 1 {
        0
    } else if n == 2 {
        1
    } else {
        // 递归的方式性能不好，n越大速度越慢
        fibonacci_number2(n - 1) + fibonacci_number2(n - 2)
    }
}
